package com.pn.controller;


import com.pn.dto.AssignRole;
import com.pn.entity.CurrentUser;
import com.pn.entity.Result;
import com.pn.entity.Role;
import com.pn.entity.User;
import com.pn.page.Page;
import com.pn.service.UserService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private TokenUtils tokenUtils;

    //分页查询用户接口
    @RequestMapping("/user-list")
    public Result userList(Page page, User user){
        Page resultPage = userService.queryUserByPage(user, page);
        return Result.ok(resultPage);
    }
    //添加用户
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //从token获取createBy
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int currentUserId = currentUser.getUserId();
        user.setCreateBy(currentUserId);
        Result result = userService.saveUser(user);
        return result;
    }
    //启用或禁用用户
    @RequestMapping("/updateState")
    public Result setUserState(@RequestBody User user){
        Result result = userService.setUserState(user);
        return result;
    }
    //根据用户id查询用户角色
    @RequestMapping("/user-role-list/{userId}")
    public Result findUserRole(@PathVariable Integer userId){
        List<Role> roleByUserId = userService.findRoleByUserId(userId);
        return Result.ok(roleByUserId);
    }

    //修改用户的角色信息
    @RequestMapping("/assignRole")
    public Result modifyUserRole(@RequestBody AssignRole assignRole){
        if (userService.modifyUserRole(assignRole) == 1){
            return Result.ok("用户角色修改成功！");
        }else {
            return Result.err(507,"用户角色修改失败！");
        }
    }
    //根据userIdList删除用户
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserInfoByUserId(@PathVariable Integer userId){
        List<Integer> userInfoList = new ArrayList<>();
        userInfoList.add(userId);
        int i = userService.deleteUserInfo(userInfoList);
        if (i == 1){
            return Result.ok("用户删除成功！");
        }else {
            return Result.err(601,"用户删除失败！");
        }
    }
    //根据userIdList删除批量用户
    @RequestMapping("/deleteUserList")
    public Result deleteUserInfoByUserIdList(@RequestBody List<Integer> userIdList){
        int i = userService.deleteUserInfo(userIdList);
        if (i == 1){
            return Result.ok("用户批量删除成功！");
        }else {
            return Result.err(602,"用户批量删除失败！");
        }
    }


    //根据userid修改用户昵称
    @RequestMapping("/updateUser")
    public Result modifyUserName(@RequestBody User user,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        Integer userId = currentUser.getUserId();
        user.setUpdateBy(userId);
        int i = userService.modifyUserName(user);
        if (i == 1){
            return Result.ok("昵称修改成功！");
        }else {
            return Result.err(603,"昵称修改失败！");
        }
    }
    //重置密码
    @RequestMapping("/updatePwd/{userId}")
    public Result updatePwd(@PathVariable Integer userId){
        int i = userService.resettingPwd(userId);
        if (i == 1){
            return Result.ok("密码重置成功！");
        }
        return Result.err(604,"密码重置失败！");
    }




}
